from PySide6.QtWidgets import QWidget, QGridLayout, QHBoxLayout, QLabel
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import  QPixmap, QIcon
from api import api
from common import config
from .widgets.dialogs import MessageOverlay
from .widgets.basewidgets import *

class LoginWindow(QWidget):
    LoginSuccess = Signal(object)

    def __init__(self):
        super().__init__()
        self.setup_ui()
        
    def setup_ui(self):
        # 设置窗口属性
        self.setWindowTitle(f"{config.SHOP_NAME}进销存管理系统 - 登录")
        self.setFixedSize(480, 420)
        self.setWindowFlags(Qt.WindowCloseButtonHint)  # 只显示关闭按钮
        self.setWindowIcon(QIcon("resources/icons/app_logo.png"))
        self.setObjectName("loginWindow")
        
        # 主布局
        main_layout = QGridLayout(self)
        main_layout.setContentsMargins(40, 40, 40, 40)
        main_layout.setSpacing(20)
        
        # Logo区域
        logo_layout = QHBoxLayout()
        logo_label = NormalLabel("")
        logo_pixmap = QPixmap("resources/images/logo.png")
        if not logo_pixmap.isNull():
            logo_label.setPixmap(logo_pixmap.scaled(100, 100, Qt.KeepAspectRatio, Qt.SmoothTransformation))
        logo_layout.addWidget(logo_label)
        logo_layout.addStretch()
        main_layout.addLayout(logo_layout, 0, 0, 1, 2)
        
        # 标题
        title_label = NormalLabel(f"{config.SHOP_NAME}进销存管理系统")
        title_label.setObjectName("loginTitle")
        title_label.setAlignment(Qt.AlignCenter)
        main_layout.addWidget(title_label, 1, 0, 1, 2)
        
        # 用户名输入
        username_label = NormalLabel("用户名:")
        username_label.setObjectName("loginLabel")
        self.username_input = NormalLineEdit()
        self.username_input.setObjectName("loginInput")
        self.username_input.setPlaceholderText("请输入用户名")
        self.username_input.returnPressed.connect(self.login)
        main_layout.addWidget(username_label, 2, 0)
        main_layout.addWidget(self.username_input, 2, 1)
        
        # 密码输入
        password_label = NormalLabel("密   码:")
        password_label.setObjectName("loginLabel")
        self.password_input = NormalLineEdit()
        self.password_input.setObjectName("loginInput")
        self.password_input.setPlaceholderText("请输入密码")
        self.password_input.setEchoMode(QLineEdit.Password)
        self.password_input.returnPressed.connect(self.login)
        main_layout.addWidget(password_label, 3, 0)
        main_layout.addWidget(self.password_input, 3, 1)
        
        # 登录按钮
        self.login_btn = BluePushButton("登录")
        self.login_btn.setObjectName("loginButton")
        self.login_btn.clicked.connect(self.login)
        main_layout.addWidget(self.login_btn, 4, 0, 1, 2)
        
    def login(self):
        username = self.username_input.text().strip()
        password = self.password_input.text().strip()
        
        if not username:
            self.username_input.setFocus()
            return
        if not password:
            self.password_input.setFocus()
            return
            
        # 调用登录API
        user = api.login(username, password)
        if user:
            # 登录成功
            self.LoginSuccess.emit(user)
            self.close()
        else:
            MessageOverlay.warning(self, "用户名或密码错误")
